package com.hmy.material.service.impl;

import com.hmy.material.domain.HmyMateriaFeedPurchase;
import com.hmy.material.domain.HmyMateriaFeedStore;
import com.hmy.material.mapper.PurchaseMapper;
import com.hmy.material.mapper.StoreMapper;
import com.hmy.material.service.PurchaseService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zxh
 * @version 1.0
 * @description: 物料进货表业务service实现类
 * @date 2024/1/29 14:35
 */
@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Resource
    private PurchaseMapper purchaseMapper;

    @Resource
    private StoreMapper storeMapper;

    @Override
    public List<HmyMateriaFeedPurchase> selectPurchaseList(Integer materiaFeedType,
                                                           String materiaFeedPurchaseProvider,
                                                           String materiaFeedPurchaseDateStart,
                                                           String materiaFeedPurchaseDateEnd) {
        return purchaseMapper.selectPurchaseList(materiaFeedType, materiaFeedPurchaseProvider, materiaFeedPurchaseDateStart, materiaFeedPurchaseDateEnd);
    }

    @Override
    public HmyMateriaFeedPurchase selectPurchaseById(Long materiaFeedPurchaseId) {
        return purchaseMapper.selectPurchaseById(materiaFeedPurchaseId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addPurchase(HmyMateriaFeedPurchase purchase) {
        //存储记录
        //获取进的物料的数量和重量，计算存储的总重量
        long storeNum = purchase.getMateriaFeedPurchaseNum() * purchase.getMateriaFeedPurchaseWeight();
        //获取存储地点和饲料类型
        String type = purchase.getMateriaFeedType();
        String position = purchase.getMateriaFeedStorePosition();
        //根据地点和类型判断存储表中是否有相同地点和类型的记录，若有则在原有的存储数量上累加，没有就增加一条记录
        //进行判断
        HmyMateriaFeedStore store = storeMapper.findStoreByTypePosition(type, position);
        if(store != null){
            long oldStoreNum = store.getMateriaFeedStoreNum();
            //说明已经有了这个记录，在原有的数量上加上新增的数量即可
            storeNum = storeNum + oldStoreNum;
            //然后根据ID更新存储记录的数量
            storeMapper.updateStoreNumById(store.getMateriaFeedStoreId(), storeNum);
        } else {
            //没有这样的存储记录，就新增一条存储记录
            HmyMateriaFeedStore feedStore = new HmyMateriaFeedStore();
            feedStore.setMateriaFeedStoreTime(new Date());
            feedStore.setMateriaFeedStorePosition(position);
            feedStore.setMateriaFeedType(type);
            feedStore.setMateriaFeedStoreNum(storeNum);
            storeMapper.addStore(feedStore);
        }
        //int i=1/0;
        return purchaseMapper.addPurchase(purchase);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePurchase(HmyMateriaFeedPurchase purchase) {
        //根据ID获取修改前的进货信息
        HmyMateriaFeedPurchase oldPurchase = purchaseMapper.selectPurchaseById(purchase.getMateriaFeedPurchaseId());
        //根据ID获取到存储记录
        HmyMateriaFeedStore feedStore = storeMapper.findStoreByTypePosition(purchase.getMateriaFeedType(), purchase.getMateriaFeedStorePosition());
        //从旧的进货记录中拿取数量和重量,并相乘
        long oldNum = oldPurchase.getMateriaFeedPurchaseNum() * oldPurchase.getMateriaFeedPurchaseWeight();
        //从传来的新的进货记录中拿取数量和重量，并相乘
        long newNum = purchase.getMateriaFeedPurchaseNum() * purchase.getMateriaFeedPurchaseWeight();

        //newNum - oldNum 就是这次修改进货记录后存储记录中的数量要更新的数量
        long num = newNum - oldNum;
        if (num > 0){
            long newStoreNum = feedStore.getMateriaFeedStoreNum() + num;
            //更新存储记录
            storeMapper.updateStoreNumById(feedStore.getMateriaFeedStoreId(), newStoreNum);
        } else {
            long newStoreNum = feedStore.getMateriaFeedStoreNum() + num;//加号是因为 num 是个负数 负负得正了就有变成加了
            //更新存储记录
            storeMapper.updateStoreNumById(feedStore.getMateriaFeedStoreId(), newStoreNum);
        }

        return purchaseMapper.updatePurchase(purchase);
    }

    @Override
    public int delPurchaseById(Long materiaFeedPurchaseId) {
        return purchaseMapper.delPurchaseById(materiaFeedPurchaseId);
    }

    @Override
    public int delPurchaseByIds(Long[] purchaseIds) {
        return purchaseMapper.delPurchaseByIds(purchaseIds);
    }
}
